package com.iwhalecloud.bss.kite.cucc.service.batch.attr;

import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IInstallationAddressService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.StandardAddressDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.StdAddrQryDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.attr.StandardAddressVO;
import com.iwhalecloud.bss.kite.cucc.common.util.KiteAssert;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * @author : xie.zhengjun
 * @className : InstallAddressProcessor
 * @description : 批量受理属性处理工具
 * @date : 2021/6/10 17:27
 */
public class BatchAttrUtil {


    public static boolean matchDistrict(String districtName, String localDistrictName) {
        return districtName.startsWith(localDistrictName);
    }

    public static String getAddressId(KiteInst inst, String custId, String fieldName, String addressName, String provinceCode,
        String cityCode, String districtCode) {
        IInstallationAddressService installationAddressService = SpringUtil.getBean(IInstallationAddressService.class);
        StdAddrQryDTO stdAddrQryDTO = new StdAddrQryDTO();
        stdAddrQryDTO.setCustId(custId);
        stdAddrQryDTO.setAddrKeyword(addressName);
        stdAddrQryDTO.setFieldName(fieldName);
        stdAddrQryDTO.setPageNum("1");
        stdAddrQryDTO.setPageNum("1");
        stdAddrQryDTO.setProvinceCode(provinceCode);
        stdAddrQryDTO.setEparchyCode(cityCode);
        stdAddrQryDTO.setCityCode(districtCode);
        StandardAddressVO standardAddressVO = installationAddressService.queryStandardAddress(stdAddrQryDTO);
        KiteAssert
            .notEmpty(standardAddressVO.getStandardAddressList(), ErrorConsts.BUSI_COMMON_ERROR, "装机地址填写不合法,！查询不到标准地址！");
        List<StandardAddressDTO> standardAddressList = JSON
            .parseArray(JSON.toJSONString(standardAddressVO.getStandardAddressList()), StandardAddressDTO.class);
        return standardAddressList.get(0).getSegmId();
    }

    /**
     * 批量受理 开通方式 特殊处理
     * @param sceneInstId
     * @param serviceOfferId
     * @param commonAttr
     */
    public static void batchOpenWayHandle(String sceneInstId, String serviceOfferId, CommonAttrVO commonAttr) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        IOperateSceneCommonService operateSceneCommonService = SpringUtil.getBean(IOperateSceneCommonService.class);
        // 批量受理订购屏蔽先核查再开通
        if (KiteStringUtils.equals(KeyConsts.IFTRUE_T,
            querySceneInstService.getExtParameterValue(sceneInstId, "isBatchAccept"))
            && KiteStringUtils.equals(serviceOfferId, "1")) {
            List<StaticAttrDO> attrValues = commonAttr.getAttrValues();
            attrValues.removeIf(attrValue -> KiteStringUtils.equals("2", attrValue.getAttrValue()));
            commonAttr.setValue("0");
            commonAttr.setDefaultValue("0");
            // 把实例里的数据修改
            KiteAttr openModeAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10005");
            List<AttrValue> attrValueList = AttrValueCache.get(commonAttr.getAttrId());
            if (Objects.nonNull(openModeAttr) && KiteListUtils.isNotEmpty(attrValueList)) {
                attrValueList.removeIf(attrValue -> KiteStringUtils.equals("2", attrValue.getAttrValue()));
                openModeAttr.setAttrValues(KiteBeanUtils.copyPropsForList(attrValueList, KiteAttrValue.class));
                openModeAttr.setValue("0");
                openModeAttr.setDefaultValue("0");
                operateSceneCommonService.setCommonAttr(sceneInstId, openModeAttr);
            }
        }
        else if (KiteStringUtils.equals(querySceneInstService.getExtParameterValue(sceneInstId, "preTaskCode"),
            "batchSIGN")) {
            // 批量受理签订 开通方式不可编辑
            commonAttr.setIsEdit(KeyConsts.IFTRUE_F);
        }
    }
}
